<?php
/**
 * Created by PhpStorm.
 * User: aron
 * Date: 2019/5/7
 * Time: 5:13 PM
 */

include_once "../../Util/DebugUtil/DebugUtil.php";

$lamp = array(
    "l" => "Linux",
    "a" => "Apache",
    "m" => "Mysql",
    "p" => "PHP",
);

// array_slice 获取数组中的一部分
DebugUtil::printDebugSeperator();
$slice = array_slice($lamp, 0, 1);
print_r($slice);
// Output:
//Array
//(
//    [l] => Linux
//)


// array_splice 删除数组中的一部分
DebugUtil::printDebugSeperator();
$opeArr = $lamp;
array_splice($opeArr, 2);
print_r($opeArr);

$opeArr = $lamp;
array_splice($opeArr, -1, 1, array("Hello", "World"));
print_r($opeArr);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => Apache
//    [m] => Mysql
//    [0] => Hello
//    [1] => World
//)


// array_combine 数组重组 key => value
DebugUtil::printDebugSeperator();
$newLamp = array_combine(array_keys($lamp), array_values($lamp));
print_r($newLamp);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => Apache
//    [m] => Mysql
//    [p] => PHP
//)


// array_merge 数组合并，键名重复会覆盖
DebugUtil::printDebugSeperator("array_merge 数组合并");
$mergedArr = array_merge($lamp, array_keys($lamp), array_values($lamp), array("p" => "PHP7"));
print_r($mergedArr);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => Apache
//    [m] => Mysql
//    [p] => PHP7
//    [0] => l
//    [1] => a
//    [2] => m
//    [3] => p
//    [4] => Linux
//    [5] => Apache
//    [6] => Mysql
//    [7] => PHP
//)


// + 操作符合并数组，键名重复不会覆盖，使用已有的
DebugUtil::printDebugSeperator("+ 操作符合并数组");
$arr = array(
    "p" => "PHP7",
    "w" => "window",
);
$mergedArr = $lamp + $arr;
print_r($mergedArr);
// Output:
//Array
//(
//    [l] => Linux
//    [a] => Apache
//    [m] => Mysql
//    [p] => PHP
//    [w] => window
//)

// array_intersect 数组交集
DebugUtil::printDebugSeperator();
$intersectedArr = array_intersect(array("PHP", "APACHE", "LINUX"), array("PHP", "APACHE", "MYSQL"));
print_r($intersectedArr);
// Output:
//Array
//(
//    [0] => PHP
//    [1] => APACHE
//)

// array_diff 数组差集
DebugUtil::printDebugSeperator();
$diffArr = array_diff(array("PHP", "APACHE", "LINUX"), array("PHP", "APACHE", "MYSQL"));
print_r($diffArr);
// Output:
//Array
//(
//    [2] => LINUX
//)


